/**
 * 
 */
package com.wixc.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;

/**
 * 一些工具类
 * 
 * @date 2015年5月20日
 */
public class MyStringUtils {

	private static Logger log = Logger.getLogger(MyStringUtils.class);

	/**
	 * 谷歌浏览器请求头
	 */
	public static final String Chrome_User_Agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36";
	/**
	 * 三星自带浏览器请求头
	 */
	public static final String Samsung_User_Agent = "Mozilla/5.0 (Linux; Android 4.2.2; GT-I9505 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36";
	/**
	 * 数据来源id，
	 */
	public static final String Spiderdatasource = "4";
	/**
	 * 采集下来的数据的状态
	 */
	public static final String Spiderdatastatus = "1";

	// 产品信息的交易类型。0充值1提现2投资产品 3投资债券 4收益5红包 6其它
	/**
	 * 0充值
	 */
	public static final short BUSINESS_TYPE_RECHARGE = 0;
	/**
	 * 1提现
	 */
	public static final short BUSINESS_TYPE_WITHDRAW = 1;
	/**
	 * 2投资产品
	 */
	public static final short BUSINESS_TYPE_INVESTMEN_PRODUCT = 2;
	/**
	 * 3投资债券
	 */
	public static final short BUSINESS_TYPE_INVESTMENT_CREDITOR = 3;
	/**
	 * 4收益
	 */
	public static final short BUSINESS_TYPE_INCOME = 4;
	/**
	 * 5红包
	 */
	public static final short BUSINESS_TYPE_ENVELOPE = 5;
	/**
	 * 6 其他
	 */
	public static final short BUSINESS_TYPE_OTHER = 6;
	/**
	 * 7 转让份额
	 */
	public static final short BUSINESS_TYPE_ZHUANRANG = 7;
	/**
	 * 8 余额转入宝宝
	 */
	public static final short BUSINESS_TYPE_YUE_2_BAOBAO = 8;
	/**
	 * 9 从宝宝类产品转入余额
	 */
	public static final short BUSINESS_TYPE_BAOBAO_2YUE = 9;

	/**
	 * 10 手续费
	 */
	public static final short BUSINESS_TYPE_SHOUXUFEI = 10;

	/**
	 * 11 冻结资金
	 */
	public static final short BUSINESS_TYPE_DONGJIE = 11;

	/**
	 * 解除资金冻结
	 */
	public static final short BUSINESS_TYPE_JIECHUDONGJIE = 12;

	/**
	 * 平台活动
	 */
	public static final short BUSINESS_TYPE_HUODONG = 13;

	/**
	 * 支出
	 */
	public static final short BUSINESS_TYPE_ZHICHU = 14;

	/**
	 * 收入
	 */
	public static final short BUSINESS_TYPE_SHOURU = 15;

	// 投资产品类型 0活期宝1理财计划2散标3债权转让,债权 4股票配资5新手标
	public static final short TYPE_PRODUCT_HuoQi = 0;
	public static final short TYPE_PRODUCT_JiHua = 1;
	public static final short TYPE_PRODUCT_SanBiao = 2;
	public static final short TYPE_PRODUCT_ZhuanRang = 3;
	public static final short TYPE_PRODUCT_ZhaiQuan = 3;
	public static final short TYPE_PRODUCT_GuPiao = 4;
	public static final short TYPE_PRODUCT_XinShouBiao = 5;

	/**
	 * 投资记录，收益记录是否能本息分离yes
	 */
	public static final String separate_flag_Y = "Y";
	/**
	 * 投资记录，收益记录是否能本息分离No
	 */
	public static final String separate_flag_N = "N";

	// 投资状态 失败 回款中 结束 转让中 已转让 筹款 审核 其他 等待付款
	/**
	 * 投资状态 失败
	 */
	public static final short INVESTMENT_STATUS_FAILE = -1;
	/**
	 * 投资状态 回款中
	 */
	public static final short INVESTMENT_STATUS_INVEST = 0;
	/**
	 * 投资状态 结束
	 */
	public static final short INVESTMENT_STATUS_END = 1;
	/**
	 * 投资状态 转让中
	 */
	public static final short INVESTMENT_STATUS_TRANSFERING = 2;
	/**
	 * 投资状态 已转让
	 */
	public static final short INVESTMENT_STATUS_TRANSFERED = 3;
	/**
	 * 投资状态 筹款
	 */
	public static final short INVESTMENT_STATUS_CHOUKUAN = 4;
	/**
	 * 投资状态 审核
	 */
	public static final short INVESTMENT_STATUS_SHENHE = 5;
	/**
	 * 投资状态 其他
	 */
	public static final short INVESTMENT_STATUS_OTHER = 6;
	/**
	 * 投资状态 等待付款
	 */
	public static final short INVESTMENT_STATUS_WAIT_FUKUAN = 7;

	// 收益状态
	/**
	 * // 待收
	 */
	public static final short INCOME_STATE_WAITGET = 1;
	/**
	 * // 已收
	 */
	public static final short INCOME_STATE_RECEIVED = 2;
	/**
	 * 逾期已收
	 */
	public static final short INCOME_STATE_OVERDUEed = 3;
	/**
	 * // 逾期未收
	 */
	public static final short INCOME_STATE_OVERDUE = 4;

	/**
	 * //提前还款
	 */
	public static final short INCOME_STATE_TIQIAN_RECEIVED = 5;

	/**
	 * 已转让
	 */
	public static final short INCOME_STATE_TRANSFER = 6;

	public static final int SpiderUpdateStatus_type_bussnise = 0;
	public static final int SpiderUpdateStatus_type_current = 1;

	/**
	 * 交易记录类型
	 */
	public static Map<String, Short> product_types = new LinkedHashMap<String, Short>();
	static {
		product_types.put("充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("在线充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("线上充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("线下充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("即时到账充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("非即时到账充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("用户充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("网银充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("宝付充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("网关充值", BUSINESS_TYPE_RECHARGE);
		product_types.put("网上充值", BUSINESS_TYPE_RECHARGE);

		product_types.put("提现", BUSINESS_TYPE_WITHDRAW);
		product_types.put("资金取现", BUSINESS_TYPE_WITHDRAW);
		product_types.put("取现", BUSINESS_TYPE_WITHDRAW);

		product_types.put("加入", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("投标", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("借出者投标成功", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("投资产品", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("投资成功", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("加入向上计划", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("投资月息通", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("投资", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("购买团团赚", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("买入", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("投资放款", BUSINESS_TYPE_INVESTMEN_PRODUCT);
		product_types.put("放款", BUSINESS_TYPE_INVESTMEN_PRODUCT);

		product_types.put("投资债券", BUSINESS_TYPE_INVESTMENT_CREDITOR);

		product_types.put("收益", BUSINESS_TYPE_INCOME);
		product_types.put("民民宝收益", BUSINESS_TYPE_INCOME);
		product_types.put("回款", BUSINESS_TYPE_INCOME);
		product_types.put("投资收益", BUSINESS_TYPE_INCOME);
		product_types.put("回收借款", BUSINESS_TYPE_INCOME);
		product_types.put("收回借款", BUSINESS_TYPE_INCOME);
		product_types.put("收回本金", BUSINESS_TYPE_INCOME);
		product_types.put("收回收益", BUSINESS_TYPE_INCOME);
		product_types.put("回收收益", BUSINESS_TYPE_INCOME);
		product_types.put("还款", BUSINESS_TYPE_INCOME);
		product_types.put("余额收益", BUSINESS_TYPE_INCOME);
		product_types.put("利息收益", BUSINESS_TYPE_INCOME);
		product_types.put("借出者收到还款", BUSINESS_TYPE_INCOME);
		product_types.put("收益分配", BUSINESS_TYPE_INCOME);
		product_types.put("付息", BUSINESS_TYPE_INCOME);
		product_types.put("收到还款", BUSINESS_TYPE_INCOME);
		product_types.put("钱包收益", BUSINESS_TYPE_INCOME);
		product_types.put("还本", BUSINESS_TYPE_INCOME);
		product_types.put("利息", BUSINESS_TYPE_INCOME);
		product_types.put("利息收回", BUSINESS_TYPE_INCOME);
		product_types.put("本金收回", BUSINESS_TYPE_INCOME);
		product_types.put("利息收入", BUSINESS_TYPE_INCOME);
		product_types.put("收款", BUSINESS_TYPE_INCOME);
		product_types.put("回收本金", BUSINESS_TYPE_INCOME);
		product_types.put("回收利息", BUSINESS_TYPE_INCOME);
		product_types.put("回收本息", BUSINESS_TYPE_INCOME);

		product_types.put("红包", BUSINESS_TYPE_ENVELOPE);
		product_types.put("红包转入", BUSINESS_TYPE_ENVELOPE);

		product_types.put("其它", BUSINESS_TYPE_OTHER);
		product_types.put("赎回", BUSINESS_TYPE_OTHER);
		product_types.put("已赎回", BUSINESS_TYPE_OTHER);
		product_types.put("已经赎回", BUSINESS_TYPE_OTHER);
		product_types.put("卖出", BUSINESS_TYPE_OTHER);
		product_types.put("已卖出", BUSINESS_TYPE_OTHER);
		product_types.put("已经卖出", BUSINESS_TYPE_OTHER);
		product_types.put("扣除冻结款", BUSINESS_TYPE_OTHER);

		product_types.put("买入份额", BUSINESS_TYPE_YUE_2_BAOBAO);
		product_types.put("零钱宝购买", BUSINESS_TYPE_YUE_2_BAOBAO);
		product_types.put("无忧宝转入", BUSINESS_TYPE_YUE_2_BAOBAO);
		product_types.put("加入活期", BUSINESS_TYPE_YUE_2_BAOBAO);
		product_types.put("民民宝资金转入", BUSINESS_TYPE_YUE_2_BAOBAO);

		product_types.put("转让份额", BUSINESS_TYPE_BAOBAO_2YUE);
		product_types.put("活期提取收益", BUSINESS_TYPE_BAOBAO_2YUE);
		product_types.put("活期提取本金", BUSINESS_TYPE_BAOBAO_2YUE);
		product_types.put("无忧宝转出", BUSINESS_TYPE_BAOBAO_2YUE);
		product_types.put("民民宝资金转出", BUSINESS_TYPE_BAOBAO_2YUE);
		product_types.put("零钱宝还本付息", BUSINESS_TYPE_BAOBAO_2YUE);
		product_types.put("无忧宝赎回结算", BUSINESS_TYPE_BAOBAO_2YUE);

		product_types.put("手续费", BUSINESS_TYPE_SHOUXUFEI);
		product_types.put("利息管理费", BUSINESS_TYPE_SHOUXUFEI);
		product_types.put("利息管理费用", BUSINESS_TYPE_SHOUXUFEI);
		product_types.put("快钱充值手续费", BUSINESS_TYPE_SHOUXUFEI);
		product_types.put("投资管理费", BUSINESS_TYPE_SHOUXUFEI);
		product_types.put("投资服务费", BUSINESS_TYPE_SHOUXUFEI);
		product_types.put("充值手续费", BUSINESS_TYPE_SHOUXUFEI);

		product_types.put("资金冻结", BUSINESS_TYPE_DONGJIE);
		product_types.put("冻结资金", BUSINESS_TYPE_DONGJIE);
		product_types.put("冻结资金", BUSINESS_TYPE_DONGJIE);
		product_types.put("投标冻结", BUSINESS_TYPE_DONGJIE);
		product_types.put("投标冻结资金", BUSINESS_TYPE_DONGJIE);
		product_types.put("投资冻结", BUSINESS_TYPE_DONGJIE);
		product_types.put("投标所冻结的资金", BUSINESS_TYPE_DONGJIE);

		product_types.put("资金解冻", BUSINESS_TYPE_JIECHUDONGJIE);
		product_types.put("解除资金冻结", BUSINESS_TYPE_JIECHUDONGJIE);
		product_types.put("解除冻结资金", BUSINESS_TYPE_JIECHUDONGJIE);
		product_types.put("投资金额解冻", BUSINESS_TYPE_JIECHUDONGJIE);
		product_types.put("撤标资金解冻", BUSINESS_TYPE_JIECHUDONGJIE);

		product_types.put("平台活动", BUSINESS_TYPE_HUODONG);
		product_types.put("用户体验金", BUSINESS_TYPE_HUODONG);
		product_types.put("活动", BUSINESS_TYPE_HUODONG);
		product_types.put("奖励", BUSINESS_TYPE_HUODONG);
		product_types.put("计划参与奖励", BUSINESS_TYPE_HUODONG);
		product_types.put("优惠券兑现", BUSINESS_TYPE_HUODONG);

	}

	/**
	 * 投资状态
	 */
	public static Map<String, Short> INVESTMENT_types = new LinkedHashMap<String, Short>();
	static {
		INVESTMENT_types.put("交易取消", INVESTMENT_STATUS_FAILE);
		INVESTMENT_types.put("投资失败", INVESTMENT_STATUS_FAILE);

		INVESTMENT_types.put("回款中", INVESTMENT_STATUS_INVEST);
		INVESTMENT_types.put("正在回款", INVESTMENT_STATUS_INVEST);
		INVESTMENT_types.put("成功", INVESTMENT_STATUS_INVEST);
		INVESTMENT_types.put("投资成功", INVESTMENT_STATUS_INVEST);
		INVESTMENT_types.put("还款中", INVESTMENT_STATUS_INVEST);
		INVESTMENT_types.put("募集成功", INVESTMENT_STATUS_INVEST);
		INVESTMENT_types.put("进行中", INVESTMENT_STATUS_INVEST);
		INVESTMENT_types.put("正常还款", INVESTMENT_STATUS_INVEST);

		INVESTMENT_types.put("筹款中", INVESTMENT_STATUS_CHOUKUAN);
		INVESTMENT_types.put("正在募集", INVESTMENT_STATUS_CHOUKUAN);
		INVESTMENT_types.put("招标中", INVESTMENT_STATUS_CHOUKUAN);
		INVESTMENT_types.put("投标中", INVESTMENT_STATUS_CHOUKUAN);

		INVESTMENT_types.put("已满", INVESTMENT_STATUS_SHENHE);
		INVESTMENT_types.put("审核", INVESTMENT_STATUS_SHENHE);
		INVESTMENT_types.put("审核中", INVESTMENT_STATUS_SHENHE);
		INVESTMENT_types.put("正在审核", INVESTMENT_STATUS_SHENHE);

		INVESTMENT_types.put("结束", INVESTMENT_STATUS_END);
		INVESTMENT_types.put("回款完毕", INVESTMENT_STATUS_END);
		INVESTMENT_types.put("已还清", INVESTMENT_STATUS_END);
		INVESTMENT_types.put("贷款结清", INVESTMENT_STATUS_END);
		INVESTMENT_types.put("已完成", INVESTMENT_STATUS_END);

		INVESTMENT_types.put("转让中", INVESTMENT_STATUS_TRANSFERING);
		INVESTMENT_types.put("已转让", INVESTMENT_STATUS_TRANSFERED);
		INVESTMENT_types.put("已经卖出", INVESTMENT_STATUS_TRANSFERED);

		INVESTMENT_types.put("待支付", INVESTMENT_STATUS_WAIT_FUKUAN);
		INVESTMENT_types.put("等待支付", INVESTMENT_STATUS_WAIT_FUKUAN);
		INVESTMENT_types.put("待付款", INVESTMENT_STATUS_WAIT_FUKUAN);
		INVESTMENT_types.put("付款", INVESTMENT_STATUS_WAIT_FUKUAN);
		INVESTMENT_types.put("等待付款", INVESTMENT_STATUS_WAIT_FUKUAN);
	}

	/**
	 * 通过正则表达式，得到需要匹配的列表
	 * 
	 * @param str
	 * @param reg
	 * @return
	 */
	public static List<String> GetList(String str, String reg) {
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		List<String> list = new ArrayList<String>();
		while (m.find()) {
			if (m.groupCount() > 0) {
				list.add(m.group(1).trim());
			} else {
				list.add(m.group().trim());
			}
		}
		return list;
	}

	/**
	 * 通过正则表达式，得到需要匹配的列表
	 * 
	 * @param str
	 * @param reg
	 * @return
	 */
	public static List<String> GetListNoHtml(String str, String reg) {
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		List<String> list = new ArrayList<String>();
		while (m.find()) {
			String find = "";
			if (m.groupCount() > 0) {
				find = m.group(1).trim();
			} else {
				find = m.group().trim();
			}
			find = find.replaceAll("<.*?>", "");
			find = find.replaceAll("\\s{1,}", " ");
			find = find.trim();
			list.add(find);
		}
		return list;
	}

	/**
	 * 通过正则表达式，得到需要匹配的一个字段
	 * 
	 * @param str
	 * @param reg
	 * @return
	 */
	public static String GetText(String str, String reg) {
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		String text = "";
		if (m.find()) {
			if (m.groupCount() > 0) {
				text = m.group(1);
			} else {
				text = m.group();
			}
		}
		return text.trim();
	}

	/**
	 * 通过正则表达式，得到需要匹配的一个字段
	 * 
	 * @param str
	 * @param reg
	 * @return
	 */
	public static String GetTextNoHtml(String str, String reg) {
		String text = GetText(str, reg);
		text = text.replaceAll("<.*?>", "");
		text = text.replaceAll("&nbsp;", " ");
		text = text.replaceAll("\\s{1,}", " ");
		return text.trim();
	}

	/**
	 * 通过正则表达式，得到需要匹配的一个字段
	 * 
	 * @param str
	 * @param reg
	 * @param defstr
	 * @return
	 */
	public static String GetTextNoHtml(String str, String reg, String defstr) {
		String text = GetTextNoHtml(str, reg);
		if ("".equals(text)) {
			text = defstr;
		}
		return text.trim();
	}

	/**
	 * 通过正则表达式，得到需要匹配的一个字段<br>
	 * long型
	 * 
	 * @param str
	 * @param reg
	 * @param defstr
	 * @return
	 */
	public static Long GetTextNoHtmlOfLong(String str, String reg, long def) {
		String text = GetTextNoHtml(str, reg);
		try {
			def = Long.parseLong(text);
		} catch (Exception e) {

		}
		return def;
	}

	/**
	 * 通过正则表达式，得到需要匹配的一个字段<br>
	 * Double型
	 * 
	 * @param str
	 * @param reg
	 * @param defstr
	 * @return
	 */
	public static double GetTextNoHtmlOfDouble(String str, String reg, double def) {
		String text = GetTextNoHtml(str, reg);
		try {
			def = Double.parseDouble(text);
		} catch (Exception e) {

		}
		BigDecimal b = new BigDecimal(def);
		def = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return Math.abs(def);
	}

	/**
	 * 通过正则表达式，得到需要匹配的一个字段<br>
	 * Integer型
	 * 
	 * @param str
	 * @param reg
	 * @param defstr
	 * @return
	 */
	public static int GetTextNoHtmlOfInteger(String str, String reg, int def) {
		String text = GetTextNoHtml(str, reg);
		try {
			def = Integer.parseInt(text);
		} catch (Exception e) {

		}
		return def;
	}

	/**
	 * 通過js引擎執行一段js
	 * 
	 * @param js
	 *            js代碼
	 * @param function
	 *            要執行的方法
	 * @param o
	 *            參數
	 * @return
	 * @throws Exception
	 */
	public static String getJs(String js, String function, Object[] o) throws Exception {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("js");
		engine.eval(js);
		Invocable jsInvoke = (Invocable) engine;
		Object result1 = jsInvoke.invokeFunction(function, o);
		return result1.toString();
	}

	/**
	 * 通过正则表达式，得到金额
	 * 
	 * @param str
	 * @param reg
	 * @param def
	 * @return
	 */
	public static double GetTextNoHtmlOfJiner(String str, String reg) {
		String text = GetTextNoHtml(str, reg);
		return GetJiner(text);
	}

	/**
	 * 金额处理<br>
	 * 将在网页中提取的金额转成double
	 * 
	 * @param jiner
	 * @return
	 */
	public static double GetJiner(String jiner) {
		if (jiner.replaceAll("[^\\d]", "").equals("")) {
			return 0;
		}
		jiner = jiner.replaceAll("￥", "").trim();
		jiner = jiner.replaceAll("¥", "").trim();
		jiner = jiner.replaceAll("\\s*", "").trim();
		jiner = jiner.trim().equals("") ? "0" : jiner;
		double a = 0;
		jiner = jiner.trim();
		if (jiner.matches(".*?\\.\\d{1,}\u5143")) {
			String newjiner = jiner.replaceAll("(.*?\\.\\d{1,})\u5143", "$1");
			newjiner = newjiner.replaceAll("[^\\d\\.]", "");
			a = Double.parseDouble(newjiner);
		} else if (jiner.matches(".*?\\.\\d{1,}\u4e07.*")) {
			String newjiner = jiner.replaceAll("\u4e07.*", "");
			Double d = 0d;
			try {
				d = Double.parseDouble(newjiner);
			} catch (Exception e) {
				newjiner = newjiner.replaceAll("[^\\d\\.]", "");
				d = Double.parseDouble(newjiner);
			}
			a = (d * 10000);
		} else if (jiner.matches(".*?万.*")) {
			String newjiner = jiner.replaceAll("[^\\d\\.]", "");
			Double amount = Double.parseDouble(newjiner);
			a = amount * 10000;
		} else if (jiner.matches(".*?\u4e07.*")) {
			String newjiner = jiner.replaceAll("[^\\d\\.]", "");
			Double amount = Double.parseDouble(newjiner);
			a = amount * 10000;
		} else if (jiner.matches("\\d{1,}\\.\\d{1,}")) {
			a = Double.parseDouble(jiner);
		} else if (jiner.matches("\\d{1,}")) {
			a = Double.parseDouble(jiner);
		} else if (jiner.matches("(\\+|-)\\d{1,}\\.\\d{1,}")) {
			a = Double.parseDouble(jiner);
		} else if (jiner.matches("(\\+|-)\\d{1,}")) {
			a = Double.parseDouble(jiner);
		} else {
			String newjiner = jiner.replaceAll("[^\\d\\.]", "");
			a = Double.parseDouble(newjiner);
		}
		BigDecimal b = new BigDecimal(a);
		a = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		a = Math.abs(a);
		return a;
	}

	public static BigDecimal getBigDecimalJiner(String jiner) {
		return new BigDecimal(GetJiner(jiner)).setScale(3, BigDecimal.ROUND_HALF_UP);
	}

	public static BigDecimal getBigDecimalLiLv(String year) {
		return new BigDecimal(getLiLv(year)).setScale(3, BigDecimal.ROUND_HALF_UP);
	}


	public static String XiaoShu2BaiFen(String str) {
		String year = "";
		double csdn = String2Double(str);
		year = XiaoShu2BaiFen(csdn);
		return year;
	}

	public static String XiaoShu2BaiFen(Double csdn) {
		String year = "";
		NumberFormat num = NumberFormat.getPercentInstance();
		num.setMaximumIntegerDigits(3);
		num.setMaximumFractionDigits(2);
		year = num.format(csdn);
		return year;
	}

	/**
	 * 将字符串转换成小数，转换失败是0
	 * 
	 * @param str
	 * @return
	 */
	public static Double String2Double(String str) {
		Double d = 0d;
		try {
			d = Double.parseDouble(str);
			BigDecimal b = new BigDecimal(d);
			d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		} catch (Exception e) {

		}
		return d;
	}

	/**
	 * 年化收益率 string<br>
	 * 如果不是范围<br>
	 * 会自动将string转int保存起来<br>
	 * 
	 * @param year
	 */
	public static double getLiLv(String year) {
		if (year == null) {
			return -1;
		}
		// 12%&nbsp;X&nbsp;110%
		if (year.matches("\\d{1,}.*?X.*?\\d{1,}%")) {
			String year1 = year.split("X")[0];
			String year2 = year.split("X")[1];
			double lilv1 = getLiLv(year1);
			double lilv2 = getLiLv(year2);
			if (lilv1 > 100) {
				lilv1 = lilv1 / 100;
			}
			if (lilv2 > 100) {
				lilv2 = lilv2 / 100;
			}
			log.info("old year : " + year + "------------>" + (lilv1 * lilv2));
			year = lilv1 * lilv2 + "";
		}

		// 想把处理加的部分改成这个
		// if (year.matches("\\d{1,}.*?\\+.*?\\d{1,}%")) {
		// String year1 = year.split("\\+")[0];
		// String year2 = year.split("\\+")[1];
		// double lilv1 = getLiLv(year1);
		// double lilv2 = getLiLv(year2);
		// if (lilv1 > 100) {
		// lilv1 = lilv1 / 100;
		// }
		// if (lilv2 > 100) {
		// lilv2 = lilv2 / 100;
		// }
		// log.info("old year : " + year + "------------>" + (lilv1 + lilv2));
		// year = lilv1 + lilv2 + "";
		// }

		double lilv = 0;
		try {
			if (year.contains("-") || year.contains("~")) {
				return -1;
			} else {
				if (year.contains("+")) {
					String[] years = year.split("\\+");
					List<Double> list = new ArrayList<Double>();
					for (String string : years) {
						if (string == null || "".equals(string)) {
							continue;
						} else {
							if (string.contains("‰")) {
								string = string.replace("‰", "").trim();
								double d = Double.parseDouble(string.trim()) / 10;
								list.add(d);
								continue;
							} else if (string.contains("%")) {
								string = string.replace("%", "").trim();
							}
							try {
								double d = Double.parseDouble(string.trim());
								list.add(d);
							} catch (Exception e) {

							}
						}
					}
					double sum = 0;
					if (list.size() > 0) {
						for (Double double1 : list) {
							sum += double1;
						}
					}
					year = sum + "";
				}

				if (year.contains("%")) {
					String nyear = year.replace("%", "");
					lilv = getLilvDouble(nyear);
				} else {
					lilv = getLilvDouble(year);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		BigDecimal b = new BigDecimal(lilv);
		double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;

	}

	/**
	 * 处理提取到的利率
	 * 
	 * @param nyear
	 * @return
	 */
	private static double getLilvDouble(String nyear) {
		nyear = nyear.replaceAll("\\s", "").trim();
		double apr = 0.00f;
		if (nyear.matches("\\d{1,}\\.\\d{1,}")) {
			apr = Double.parseDouble(nyear);
		} else if (nyear.matches("[^\\d]*\\d{1,}\\.\\d{1,}[^\\d]*")) {
			nyear = nyear.replaceAll("[^\\d]*(\\d{1,}\\.\\d{1,})[^\\d]*", "$1");
			apr = Double.parseDouble(nyear);
		} else if (nyear.matches("[^\\d]*\\d{1,}[^\\d]*")) {
			nyear = nyear.replaceAll("[^\\d]*(\\d{1,})[^\\d]*", "$1");
			apr = Double.parseDouble(nyear);
		}
		return apr;
	}

	/**
	 * 字符串转换成日期<br>
	 * 
	 * @param str
	 * @param formatstr
	 *            yyyy-MM-dd yyyy-MM-dd HH:mm:ss.ms "yyyy-MM-dd HH:mm:ss"
	 * @return date
	 */
	public static Date StrToDate(String str, String formatstr) {

		SimpleDateFormat format = new SimpleDateFormat(formatstr);
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			System.err.println(e.getMessage() + e);
			System.err.println(str);
			System.err.println(formatstr);
		}
		return date;
	}

	/**
	 * 处理URL
	 * 
	 * @param refer
	 * @param url
	 * @return
	 */
	public static String tidyUrl(String refer, String url) {
		String nurl = "";
		try {
			if (!url.startsWith("http")) {
				URL u = new URL(refer);
				nurl = new URL(u, url).toString();
			} else {
				nurl = url;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return nurl;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> json2Map(String json) {
		Gson gson = new Gson();
		Map<String, Object> map = null;
		try {
			map = gson.fromJson(json, Map.class);
		} catch (Exception e) {
			System.out.print("MyStringUtils.json2Map() ------>" + e.getMessage());
		}
		return map;
	}

	/**
	 * 日期转换成字符串<br>
	 * yyyy-MM-dd HH:mm:ss.ms
	 * 
	 * @param date
	 *            long
	 * @return str
	 */
	public static String LongToDateStr(Long date, String formatstr) {
		return DateToStr(new Date(date), formatstr);
	}

	/**
	 * 日期转换成字符串<br>
	 * yyyy-MM-dd HH:mm:ss.ms
	 * 
	 * @param date
	 * @return str
	 */
	public static String DateToStr(Date date, String formatstr) {
		SimpleDateFormat format = new SimpleDateFormat(formatstr);
		String str = format.format(date);
		return str;
	}

	/**
	 * 交易记录使用
	 * 
	 * @param type
	 * @return
	 */
	public static Short GetType(String type) {
		Short s = product_types.get(type.trim());
		return s != null ? s : BUSINESS_TYPE_OTHER;
	}

	/**
	 * 投资记录使用
	 * 
	 * @param type
	 * @return
	 */
	public static Short GetInvestment_types(String type) {
		Short s = INVESTMENT_types.get(type);
		return s != null ? s : INVESTMENT_STATUS_OTHER;
	}

	/**
	 * 数组第一个参数是时间，第二个是时间单位 1:月 2:天 0:年
	 * 
	 * @param circle
	 * @return
	 */
	public static Double[] getPeroid(String circle) {
		Double peroid = 0d;
		Double peroid_type = 2d;
		if (circle == null) {
			return new Double[] { peroid, peroid_type };
		}
		if (circle.matches(".*\\u6708") || circle.contains("月") || circle.contains("\u6708")) {
			// 月
			peroid_type = 1d;
		} else if (circle.matches(".*\\u5929") || circle.contains(".*\\u65e5") || circle.contains("\u5929") || circle.matches("\u65e5")
				|| circle.contains("日") || circle.contains("天")) {
			// 日，天
			peroid_type = 2d;
		} else if (circle.matches(".*\\u5e74") || circle.contains("年") || circle.contains("\u5e74")) {
			// 年
			peroid_type = 0d;
		} else {
			peroid_type = 2d;
		}

		if (circle != null && !circle.equals("")) {
			if (circle.contains(".")) {
				circle = circle.replaceAll("(\\d{1,}.\\d{1,}).*", "$1");
			} else {
				circle = circle.replaceAll("[^\\d]", "");
			}
			peroid = Double.parseDouble(circle);
		}
		return new Double[] { peroid, peroid_type };

	}

	/**
	 * 读取打包的js代码
	 * 
	 * @param filepath
	 * @return
	 */
	public static String readJs(String filepath) {
		String a = "";
		BufferedReader br = null;
		InputStream is = null;
		if (!filepath.startsWith("/")) {
			filepath = "/" + filepath;
		}
		try {
			is = MyStringUtils.class.getResourceAsStream(filepath);

			StringBuilder strBlder = new StringBuilder("");
			br = new BufferedReader(new InputStreamReader(is));
			String line = "";
			while (null != (line = br.readLine())) {
				strBlder.append(line + "\n");
			}
			a = strBlder.toString();
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (br != null) {
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return a;
	}

	/**
	 * 将资源中的证书读出来
	 * 
	 * @param filepath
	 * @return
	 */
	public static byte[] readCer(String filepath) {
		byte[] b = null;
		InputStream is = null;
		if (!filepath.startsWith("/")) {
			filepath = "/" + filepath;
		}
		try {
			is = MyStringUtils.class.getResourceAsStream(filepath);
			b = new byte[is.available()];
			is.read(b);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return b;
	}

	/**
	 * 浮点型转整形
	 * 
	 * @param d
	 * @return
	 */
	public static int getDouble2Int(double d) {
		int a = Integer.parseInt(new java.text.DecimalFormat("0").format(d));
		return a;
	}

	/**
	 * 浮点型转long
	 * 
	 * @param d
	 * @return
	 */
	public static long getDouble2Long(double d) {
		long a = Long.parseLong(new java.text.DecimalFormat("0").format(d));
		return a;
	}

	/**
	 * 
	 * @param d
	 * @return
	 */
	public static short getDouble2Short(double d) {
		short a = Short.parseShort(new java.text.DecimalFormat("0").format(d));
		return a;
	}

	/**
	 * unicode转utf-8
	 * 
	 * @param d
	 * @return
	 */
	public static String getUnicode2Utf8(String uni) {
		String reulst = "";
		try {
			reulst = new String(uni.getBytes("utf-8"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return reulst;
	}

	/**
	 * 计算总页数
	 * 
	 * @param count
	 * @param size
	 * @return
	 */
	public static int getTotalPage(int count, int size) {
		return count % size == 0 ? count / size : ((count / size) + 1);
	}

	/**
	 * 当天yyyy-DD-mm格式的long
	 * 
	 * @param count
	 * @param size
	 * @return
	 */
	public static long getDateTodayLong() {

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long d = cal.getTimeInMillis();
		return d;
	}

	/**
	 * 获取Date偏移天数
	 * 
	 * @param count
	 * @param size
	 * @return
	 */
	public static Date getDateByDayIndex(Date date, int day) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, day);
		return cal.getTime();
	}

	/**
	 * long变为当天yyyy-DD-mm格式的long
	 * 
	 * @param count
	 * @param size
	 * @return
	 */
	public static long getDateLong2Long(long time) {

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long d = cal.getTimeInMillis();
		return d;
	}

	/**
	 * 通过url解析参数
	 * 
	 * @param url
	 * @return
	 */
	public static Map<String, String> getPramByUrl(String url) {
		Map<String, String> parm = new HashMap<String, String>();
		String query = "";
		try {
			URL aURL = new URL(url);
			query = aURL.getQuery();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		List<NameValuePair> list = URLEncodedUtils.parse(query, Charset.forName("UTF-8"));
		for (int i = 0; i < list.size(); i++) {
			parm.put(list.get(i).getName(), list.get(i).getValue());
		}
		return parm;
	}

	/**
	 * 获得今天的字符串形式<br>
	 * 默认格式yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getTodayDateStr(String format) {
		if (null == format || "".equals(format)) {
			format = "yyyy-MM-dd";
		}
		Calendar c = Calendar.getInstance();
		String today = MyStringUtils.DateToStr(c.getTime(), format);
		return today;
	}

	/**
	 * 默认格式yyyy-MM-dd
	 * 
	 * @param month
	 *            几个月之前
	 * @param format
	 *            格式 yyyy-MM-dd HH:mm:ss.ms
	 * @return
	 */
	public static String getMonthBefore(int month, String format) {
		if (null == format || "".equals(format)) {
			format = "yyyy-MM-dd";
		}
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -month);
		String start = MyStringUtils.DateToStr(c.getTime(), format);
		return start;
	}

	/**
	 * 默认格式yyyy-MM-dd
	 * 
	 * @param month
	 *            几个月之后
	 * @param format
	 *            格式 yyyy-MM-dd HH:mm:ss.ms
	 * @return
	 */
	public static String getMonthAfter(int month, String format) {
		if (null == format || "".equals(format)) {
			format = "yyyy-MM-dd";
		}
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, month);
		String start = MyStringUtils.DateToStr(c.getTime(), format);
		return start;
	}

	/**
	 * 计算收益(不包含本金)
	 * 
	 * @param money
	 * @param yearRate
	 * @param period
	 *            　数组第一个参数是时间，第二个是时间单位 1:月 2:天 0:年
	 * @return
	 */
	public static double getShouYi(double money, double yearRate, Double[] period) {
		double days = 1;
		if (period[1] == 1) { // 1:月
			days = period[0] * 30;
		} else if (period[1] == 2) { // 2:天
			days = period[0] * 1;
		} else {
			days = period[0] * 365;
		}
		double shouyi = (money * yearRate / 100) * days / 365;
		return shouyi;
	}

	/**
	 * 根据投资期限，获得结束日期
	 * 
	 * @param start_date
	 * @param period
	 * @return
	 */
	public static Date getEndDate(Date start_date, Double[] period) {
		double days = 1;
		if (period[1] == 1) { // 1:月
			days = period[0] * 30;
		} else if (period[1] == 2) { // 2:天
			days = period[0] * 1;
		} else {
			days = period[0] * 365;
		}
		Date date = getDateByDayIndex(start_date, MyStringUtils.getDouble2Int(days));
		return date;
	}

	public static void sleep(Logger log, long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e1) {
			log.error(e1.getMessage());
		}
	}

	public static boolean isEmpty(String str) {
		if (null == str || str.length() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 * 计算两个日期之间的天数
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int daysOfTwo(Date startDate, Date endDate) {
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(startDate);
		int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
		aCalendar.setTime(endDate);
		int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
		return day2 - day1;
	}

	/**
	 * 获得Boolean值
	 * 
	 * @param o
	 * @return
	 */
	public static boolean readBoolean(Object o) {
		if (o == null) {
			return false;
		}
		String a = o.toString();
		if ("".equals(a) || a.equals("0") || a.equalsIgnoreCase("false") || a.equalsIgnoreCase("off")) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获得相差天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int getDays(Date startDate, Date endDate) {
		if (null == startDate || null == endDate) {
			return -1;
		}
		long intervalMilli = endDate.getTime() - startDate.getTime();
		return (int) (intervalMilli / (24 * 60 * 60 * 1000));
	}
	/**
	 * 获得相差月数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int getMonth(Date startDate, Date endDate) {
		if (null == startDate || null == endDate) {
			return -1;
		}
		long intervalMilli = endDate.getTime() - startDate.getTime();
	    int days = (int) (intervalMilli / (24 * 60 * 60 * 1000));
	    int month = days%31 == 0 ? days/31 : (days/31) +1;
		return month; 
	}
	public static void main(String[] args) {
		String s = getUnicode2Utf8("\u52a0\u5165\u4f18\u9009\u7406\u8d22\u8ba1\u5212M030125\u671f");
		System.out.println(s);
		long st = MyStringUtils.StrToDate("20151225", "yyyyMMdd").getTime();
		System.out.println(getLiLv("11.999999999999998%  X 110%"));
		System.out.println(getLiLv("11.999999999999998%  + 110%"));
		System.out.println(st);
	}

}
